//
//  HangmanModeViewController.m
//  Multiview
//
//  Created by Moo Chewachatrekasem on 10-03-13.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "HangmanModeViewController.h"
#import "MultiviewAppDelegate.h"

NSString *word;
NSArray *wordList;

@implementation HangmanModeViewController

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	image.image = [UIImage imageNamed:@"monster.jpg"];
	[image setNeedsDisplay];
	pie1.image = [UIImage imageNamed:@"pie.jpg"];
	[pie1 setNeedsDisplay];
	pie2.image = [UIImage imageNamed:@"pie.jpg"];
	[pie2 setNeedsDisplay];
	pie3.image = [UIImage imageNamed:@"pie.jpg"];
	[pie3 setNeedsDisplay];
	pie4.image = [UIImage imageNamed:@"pie.jpg"];
	[pie4 setNeedsDisplay];
	

	
	[self createWordList];
	
	int randomIndex = random() % [wordList count];
	
	word = [[NSString alloc] initWithString:[wordList objectAtIndex: randomIndex]];
	[wordList retain];
	NSLog(@"word: %@", word);
	
	NSLog(@"%@", word);
	
	for (int i = 0; i < [word length]; i++) {
		label.text = [NSString stringWithFormat:@"%@%@", label.text, @"-"];
	}
	
}
			 
- (IBAction) returnToMenu {
	MultiviewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	[appDelegate displayView:2];
}

- (IBAction) selectLetter:(id)sender {
	//label.text = [[sender titleForState:UIControlStateNormal] lowercaseString];
	NSString *letter = [[NSString alloc] initWithString:[[sender titleForState:UIControlStateNormal] lowercaseString]];
	NSString *txtLabel = [[NSString alloc] initWithString:label.text];
	[sender setTitle:@"-" forState:UIControlStateNormal];
	[sender setEnabled:NO];
	label.text = @"";
	BOOL right = NO;
	//wrong++;
	//NSLog(@"%@cxzcxzczxccxz",txtLabel);
	//NSLog(@"%@",[word substringToIndex:4]);
	for (int i = 0; i <[word length]; i++) {
		//NSLog(@"%@", [[word substringFromIndex:i] substringToIndex:1]);
		//NSString *tmp = [[txtLabel substringFromIndex:i] substringToIndex:1];
		//NSLog(@"%@", tmp);
		//NSLog(@"%@",[[word substringToIndex:i] substringToIndex: 1]);
		
		if([[[word substringFromIndex:i] substringToIndex:1] isEqual:letter]){
			label.text = [NSString stringWithFormat:@"%@%@", label.text, letter];
			right = YES;
		}else{
			label.text = [NSString stringWithFormat:@"%@%@", label.text, [[txtLabel substringFromIndex:i] substringToIndex:1] ];
		}
		
	}
	
	if(!right){
		wrong++;
	}
		 
		 
	[self reducePie];
	
	if(wrong >= 4){
		[self exitWrong];
		[self returnToMenu];
	}
	
	if ([label.text isEqualToString: word]){
		[self exitRight];
		[self returnToMenu];
	}
	
	
}

- (void)exitRight{
	NSString *resultMessage = [NSString stringWithFormat:@"Congratulation"];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Win!" message:[NSString stringWithFormat:@"%@",resultMessage]
												   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
	[alert show];
	[alert release];
	
}

- (void)exitWrong{
	NSString *resultMessage = [NSString stringWithFormat:@"Please Try Again"];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lose!" message:[NSString stringWithFormat:@"%@",resultMessage]
												   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
	[alert show];
	[alert release];
	
}
- (IBAction) submit{
	if ([answer.text isEqualToString: word]){
		[self exitRight];
	}else{
		[self exitWrong];
	}
	[self returnToMenu];
}

- (void)reducePie{
	switch (wrong) {
		case 1:
			pie1.image = nil;
			break;
		case 2:
			pie2.image = nil;
			break;
		case 3:
			pie3.image = nil;
			break;
		case 4:
			pie4.image = nil;
			break;
		
	}
	
}

- (void) createWordList {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	
	
	//get path to your file
	NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"wordlist.txt"];
	
	if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
	{
		NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"wordlist" ofType:@"txt"];
		NSFileManager *fileManager = [NSFileManager defaultManager];
	}
	
	NSString *myString = [[NSString alloc] initWithContentsOfFile:myPathDocs encoding:NSUTF8StringEncoding error:NULL];
	
	NSLog(@"ASSAASAS %@", myString);
	
	
	wordList = [myString componentsSeparatedByString:@"\n"];
	NSLog(@"ASSAASAS %@", [wordList objectAtIndex: 0]);
	[myString release];
	
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
